//
//  HBaseLiveController.m
//  linktuIOS
//
//  Created by hejunsong on 2017/2/5.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HBaseLiveController.h"
#import "HeartAnimation.h"
#import <CommonCrypto/CommonCryptor.h>

@interface HBaseLiveController ()

@property (nonatomic, strong) UIImageView *topImageView;
@property (nonatomic, strong) UIImageView *bottomImageView;

@property (nonatomic, strong) UIActivityIndicatorView *animationView;



@end

@implementation HBaseLiveController

-(instancetype)initWithType:(LiveControllertype)type
{
    if(self = [super init])
    {
        _conteollerType = type;
        _livecontentMode = UIViewContentModeScaleAspectFill; 
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self makemainScrollView];
    [self makeLeftUI];
    [self makePublicUI];

    [self addSwitchLiveGesture];

    
    [IQKeyboardManager sharedManager].enable = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [[UIApplication sharedApplication] setIdleTimerDisabled: YES];
    //    //1.监听键盘弹出，把inputToolbar(输入工具条)往上移
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //2.监听键盘退出，inputToolbar恢复原位
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbWillHide:) name:UIKeyboardWillHideNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleInterruption:)
                                                 name:AVAudioSessionInterruptionNotification
                                               object:[AVAudioSession sharedInstance]];

    
}

-(void)reoladCover
{
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:self.toplivemodel.cover] placeholderImage:nil];
    [self.bottomImageView sd_setImageWithURL:[NSURL URLWithString:self.bottomlivemodel.cover] placeholderImage:nil];
    [self.currentImageView sd_setImageWithURL:[NSURL URLWithString:self.livemodel.cover] placeholderImage:nil];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if(self.conteollerType == LiveControllertypeAudience){
        if(WIDTH > HEIGHT)
        {
            self.messageView.hidden = YES;
        }else
        {
            self.messageView.hidden = NO;
        }
    }
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    //离开页面的时候横屏处理
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
     self.navigationController.navigationBar.hidden = NO;
//    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    delegate.allowRotate = 0;
//    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
//        SEL selector = NSSelectorFromString(@"setOrientation:");
//        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
//        [invocation setSelector:selector];
//        [invocation setTarget:[UIDevice currentDevice]];
//        int val = UIInterfaceOrientationPortrait;
//        [invocation setArgument:&val atIndex:2];
//        [invocation invoke];
//    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
    self.navigationController.navigationBar.hidden = YES;
}

-(void)makemainScrollView
{
    _mainScrollView = [[HLiveScrollView alloc] init];
    _mainScrollView.bounces = NO;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.backgroundColor = [UIColor clearColor];
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    [_mainScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:@"_mainScrollViewID"];
    
    
    self.view.backgroundColor = [UIColor blackColor];
    
    //    _mainScrollView.scrollEnabled = NO;
    
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor clearColor];
    
    
    
    [self.mainScrollView addSubview:_contentView];
    
    [self.view addSubview:_mainScrollView];
    
    [_mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.mainScrollView);
        make.height.equalTo(self.mainScrollView);
    }];
    
    
    
   if(self.conteollerType != LiveControllertypeLive){
    _topImageView = [UIImageView new];
    _bottomImageView = [UIImageView new];
    _currentImageView = [UIImageView new];
    
    _topImageView.contentMode     = UIViewContentModeScaleAspectFill;
    _bottomImageView.contentMode  = UIViewContentModeScaleAspectFill;
    _currentImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    _topImageView.clipsToBounds = YES;
    _bottomImageView.clipsToBounds = YES;
    _currentImageView.clipsToBounds = YES;
    
    [self.view addSubview:_topImageView];
    [self.view addSubview:_bottomImageView];
    [self.view insertSubview:_currentImageView atIndex:0];
    
    //    _topImageView.backgroundColor = [UIColor redColor];
    //    _bottomImageView.backgroundColor = [UIColor redColor];
    
    [_topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo (self.view.mas_width);
        make.bottom.equalTo(self.view.mas_top).offset(0);
        make.height.equalTo(self.view.mas_height);
        make.left.equalTo(self.view.mas_left).offset(0);
        
    }];
    [_bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo (self.view.mas_width);
        make.top.equalTo(self.view.mas_bottom).offset(0);
        make.height.equalTo(self.view.mas_height);
        make.left.equalTo(self.view.mas_left).offset(0);
        
    }];
    
    [_currentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo (self.view.mas_width);
        make.top.equalTo(self.view.mas_top).offset(0);
        make.height.equalTo(self.view.mas_height);
        make.left.equalTo(self.view.mas_left).offset(0);
        
    }];
    [self reoladCover];
    
       
    }
}

-(void)makePublicUI
{
    _mainView =  [[UIView alloc] initWithFrame:CGRectMake(WIDTH,0, WIDTH, HEIGHT)];
    _mainView.clipsToBounds = YES;
    _mainView.backgroundColor = [UIColor clearColor];
    _anchorprofileView = [[HLiveProfileView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _goldView = [[HLivegoldView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _goldView.hidden = YES;
  
    _topAudicenceView = [[HLiveTopAudienceView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _messageView = [[HLiveMessageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _livetitleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _livetitleLable.textColor = [UIColor whiteColor];
    
    _toolView = [[HLiveToolView alloc] initWithFrame:CGRectMake(0, HEIGHT - 50, WIDTH, 45) withStyle:HLiveToolViewstylaudiences];
    _inputView = [[HLiveInputView alloc] initWithFrame:CGRectMake(0, 200, WIDTH, 50)];
    _inputView.pay_barrage_amount = 1;
    _lightNumberView = [[HLiveLightView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _lightNumberView.hidden = YES;
    _hostOnAirView = [[OnAirTimeView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _hostOnAirView.hidden = YES;
    
   
    UIView *tuchView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, WIDTH, HEIGHT - 100 - 210)];
    UITapGestureRecognizer *gister = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LightClick)];
    [tuchView addGestureRecognizer:gister];
   
    switch (_conteollerType) {
        case LiveControllertypeAudience:
        {
            _toolView.style = HLiveToolViewstylaudiences;
            _toolView.isGameLive = NO;
            [_mainView addSubview:tuchView];
            [_mainView addSubview:_hostOnAirView];
            break;
        }
        case LiveControllertypeLive:
        {
            _toolView.style = HLiveToolViewstyleanchor;
            [_mainView addSubview:tuchView];
            [_mainView addSubview:_hostOnAirView];
            break;
        }
        case LiveControllertypeReplay:  {
            _toolView.style = HLiveToolViewstyleplayblack;
            [_mainView addSubview:tuchView];
            [_mainView addSubview:_hostOnAirView];
//          self.messageView.hidden = YES;
            break;
        }
        default:
            break;
    }
    
    _fllowingAnchorButton = [UIButton new];
    _closeButton = [UIButton new];
    _closeFullScreenButton = [UIButton new];
    
    [_fllowingAnchorButton setImage:[UIImage imageNamed:@"icon_follow_Unchecked"] forState:UIControlStateNormal];
    [_fllowingAnchorButton setImage:[UIImage imageNamed:@"icon_follow_checked"] forState:UIControlStateSelected];
    [_fllowingAnchorButton addTarget:self action:@selector(FllowingAnchor) forControlEvents:UIControlEventTouchUpInside];
    
    
    [_closeButton setImage:[UIImage imageNamed:@"icon_Sign-out"] forState:UIControlStateNormal];
    [_closeButton addTarget:self action:@selector(CloseRoomClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [_closeFullScreenButton setImage:[UIImage imageNamed:@"icon_Full_narrow"] forState:UIControlStateNormal];
    
    [_closeFullScreenButton addTarget:self action:@selector(CloseFullScreenClick:) forControlEvents:UIControlEventTouchUpInside];
    _closeFullScreenButton.hidden = YES;
    [self.view addSubview:_closeFullScreenButton];
    [_closeFullScreenButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@40);
        make.width.equalTo(@40);
        make.right.mas_equalTo(self.view.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-10);
        
    }];

//    _shareView = [MTShareView shareView];
    
    [self.contentView addSubview:_mainView];
    
    
    [_mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_mainScrollView.mas_width);
        make.left.mas_equalTo(_leftView.mas_right).offset(0);
        make.height.equalTo(_contentView.mas_height);
        make.top.mas_equalTo(_contentView.mas_top).offset(0);
    }];
    
    [_mainView addSubview:_anchorprofileView];
    [_mainView addSubview:_goldView];
    [_mainView addSubview:_topAudicenceView];
    [_mainView addSubview:_messageView];
    [_mainView addSubview:_livetitleLable];
    [_mainView addSubview:_toolView];
    [_mainView addSubview:_inputView];
    
    [_mainView addSubview:_fllowingAnchorButton];
    [_mainView addSubview:_closeButton];
    [_mainView addSubview:_lightNumberView];
    
    [_mainView addSubview:self.replayProgressView];

    
    [_anchorprofileView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mainView.mas_top).offset(30);
        make.left.mas_equalTo(_mainView.mas_left).offset(10);
        make.height.mas_equalTo(@38);
    }];
    
    [_goldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_anchorprofileView.mas_left).offset(0);
        make.top.mas_equalTo(_anchorprofileView.mas_bottom).offset(10);
        make.height.mas_equalTo(@22);
    }];
    
    [_lightNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_anchorprofileView.mas_left).offset(0);
        make.top.mas_equalTo(_goldView.mas_bottom).offset(10);
        make.height.mas_equalTo(@18);
    }];
    
    [_hostOnAirView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_anchorprofileView.mas_left).offset(0);
        make.top.mas_equalTo(_lightNumberView.mas_bottom).offset(10);
        make.height.mas_equalTo(@18);
    }];
    
    CGFloat fllowW = 36;
    if(_conteollerType == LiveControllertypeLive)
    {
        fllowW = 0;
    }
    [_fllowingAnchorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@38);
        make.width.mas_equalTo(@(fllowW));
        make.left.mas_equalTo(_anchorprofileView.mas_right).offset(10);
        make.centerY.mas_equalTo(_anchorprofileView.mas_centerY).offset(0);
    }];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@38);
        make.width.mas_equalTo(@0);
        make.right.mas_equalTo(_mainView.mas_right).offset(-10);
        make.centerY.mas_equalTo(_anchorprofileView.mas_centerY).offset(0);
    }];
    [_topAudicenceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_anchorprofileView.mas_centerY).offset(0);
        make.height.mas_equalTo(@38);
        make.right.mas_equalTo(_closeButton.mas_left).offset(-5);
        make.left.mas_equalTo(_fllowingAnchorButton.mas_right).offset(5);
    }];
    
    [_messageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@150);
        make.bottom.mas_equalTo(_inputView.mas_top).offset(- 40 - bottomBaroffset - 20);
        make.right.mas_equalTo(_mainView.mas_right).offset(-50);
        make.left.mas_equalTo(_mainView.mas_left).offset(10);
    }];
    
    [_livetitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(@(25));
        make.right.mas_equalTo(_mainView.mas_right).offset(-50);
        make.left.mas_equalTo(_mainView.mas_left).offset(10);
        make.bottom.mas_equalTo(_messageView.mas_top).offset(0);
    }];
    
    
    [_inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_mainView.mas_right).offset(0);
        make.left.mas_equalTo(_mainView.mas_left).offset(0);
        make.height.mas_equalTo(@50);
        make.top.mas_equalTo(_mainView.mas_bottom).offset(0);
    }];
    

    [_toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_mainView.mas_right).offset(0);
        make.left.mas_equalTo(_mainView.mas_left).offset(0);
        make.height.mas_equalTo(@40);
        make.bottom.mas_equalTo(_mainView.mas_bottom).offset(-10 - bottomBaroffset);
    }];
    
    [self.replayProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@45);
        make.left.mas_equalTo(self.mainView.mas_left).offset(10);
        make.right.mas_equalTo(self.mainView.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.toolView.mas_top).offset(-10);
    }];
    
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_mainView.mas_right);
    }];
    
}


-(void)makeLeftUI
{
    _leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    _leftView.backgroundColor = [UIColor clearColor];
//    _leftanchorprofileView =  [[HLiveProfileView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//    _leftAudicenceView = [[HLiveLeftAudienceView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//
//    UIButton *but = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//    [but setImage:[UIImage imageNamed:@"icon_address_focus"] forState:UIControlStateNormal];
//    [but setTitle:@"     " forState:UIControlStateNormal];
//    but.titleLabel.font = [UIFont systemFontOfSize:14];
//    self.addressButton = but;
//    if(self.conteollerType == LiveControllertypeLive)
//    {
//        self.addressButton.hidden = YES;
//    }
    
    [_contentView addSubview:_leftView];
//    [_leftView addSubview:_leftanchorprofileView];
//    [_leftView addSubview:but];
//    [_leftView addSubview:_leftAudicenceView];
    
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(_contentView.mas_height);
        make.left.mas_equalTo(_contentView.mas_left).offset(0);
        make.width.mas_equalTo(_mainScrollView.mas_width);
        make.top.mas_equalTo(_contentView.mas_top).offset(0);
    }];
    
    
    
    
//    [_leftanchorprofileView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_leftView.mas_top).offset(30);
//        make.left.mas_equalTo(_leftView.mas_left).offset(10);
//        make.height.mas_equalTo(@38);
//    }];
    
    
//    [but mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_leftanchorprofileView.mas_bottom).offset(100);
//        make.left.mas_equalTo(_leftView.mas_left).offset(10);
//    }];
    
//    [_leftAudicenceView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(_leftView.mas_left).offset(0);
//        make.right.mas_equalTo(_leftView.mas_right).offset(0);
//        make.bottom.mas_equalTo(_leftView.mas_bottom).offset(0);
//        make.top.mas_equalTo(_leftView.mas_top).offset(200);
//    }];
}
-(void)hiddenWhenisAudite
{
    
    
}



-(void)addSwitchLiveGesture
{
    if(self.allowSlideSwitch){
        UIPanGestureRecognizer *pangestur = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(SwitchLiveClick:)];
        self.panGesture = self.panGesture;
        [self.view addGestureRecognizer:pangestur];
    }
}
-(void)SwitchLiveClick:(UIPanGestureRecognizer *)gesture
{
}

-(void)CloseRoomClick:(UIButton *)but
{

}
-(void)CloseFullScreenClick:(UIButton *)but
{
    self.mainScrollView.hidden = NO;
    but.hidden = YES;
}
-(void)LightClick
{

}
-(void)closegameClick:(UIButton *)but
{
    _isshowGameAndGiftView = but.selected;
    but.selected = !but.selected;
    if(but.selected)
    {
        [self.gameAndGiftContentView mas_updateConstraints:^(MASConstraintMaker *make) {
             make.bottom.mas_equalTo(_mainView.mas_bottom).offset(RelativeValue(201));
        }];
        [_toolView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(_gameAndGiftContentView.mas_top).offset(-10);
        }];
        [UIView animateWithDuration:0.5 animations:^{
          [self.view layoutIfNeeded];
        }];
    }else
    {
        [self.gameAndGiftContentView mas_updateConstraints:^(MASConstraintMaker *make) {
             make.bottom.mas_equalTo(_mainView.mas_bottom).offset(0);
        }];
        [_toolView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(_gameAndGiftContentView.mas_top).offset(0);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            [self.view layoutIfNeeded];
        }];
    }
}
-(void)setIsshowGameAndGiftView:(BOOL)isshowGameAndGiftView
{
    _isshowGameAndGiftView = isshowGameAndGiftView;
    self.closegameButton.selected = !isshowGameAndGiftView;

}

-(void)gamehelpClick:(UIButton *)but
{
    NSLog(@"you xi bang zu");
   

}



//判断是否关注主播
-(void)judgeFollowhost
{
    
   if(self.livemodel.anchor.follow)
   {
       [self.fllowingAnchorButton mas_updateConstraints:^(MASConstraintMaker *make) {
           make.width.equalTo(@0);
           make.height.equalTo(@0);
       }];
   }else
   {
       [self.fllowingAnchorButton mas_updateConstraints:^(MASConstraintMaker *make) {
           make.width.equalTo(@40);
           make.height.equalTo(@40);
       }];
   }
}

-(void)FllowingAnchor
{
    self.fllowingAnchorButton.selected = YES;
    [self.fllowingAnchorButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@0);
        make.height.equalTo(@0);
    }];
    //添加动画
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
    [[MTUserApiManager manager] followUserV2WithUserId:self.livemodel.anchor.userId follow:YES completion:^(MTError *mterror) {
        if(mterror == nil)
        {
            self.livemodel.anchor.follow = YES;
        }
    }];
}

#pragma mark -- 用户点亮的动画
-(void)lightenAnimation
{
    UIImageView *imageview = [[UIImageView alloc]init];
    imageview.frame = CGRectMake(286.5/375 * WIDTH, 611.5/667 * HEIGHT, 0, 0);
    imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"like_%d_live", arc4random()%6]];
    [self.mainView addSubview:imageview];
    //创建动画组
    CAAnimationGroup *animationGroup = [[CAAnimationGroup alloc]init];
    animationGroup.duration = 3;
    animationGroup.animations = [[HeartAnimation singleHeart] getAnimationArray];
    [imageview.layer addAnimation:animationGroup forKey:nil];
}





#pragma  mark observe
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if([keyPath isEqualToString:@"contentSize"])
    {
        self.mainScrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
}
#pragma mark 键盘显示时会触发的方法
-(void)kbWillShow:(NSNotification *)noti{
    //1.获取键盘高度
    //1.1获取键盘结束时候的位置
    CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbHeight = kbEndFrm.size.height;
    [self.inputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mainView.mas_bottom).offset(-kbHeight - 50);
    }];
    
    
    [self.messageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_inputView.mas_top).offset(0);
    }];
    
    //添加动画
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}

#pragma mark 键盘退出时会触发的方法
-(void)kbWillHide:(NSNotification *)noti{
    [self.inputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mainView.mas_bottom).offset(0);
    }];
    [self.messageView mas_updateConstraints:^(MASConstraintMaker *make) {
       make.bottom.mas_equalTo(_inputView.mas_top).offset(- 60 - bottomBaroffset);
    }];
    //添加动画
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)handleInterruption:(NSNotification *)notification {
    if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
        NSLog(@"Interruption notification");
        
        if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
            NSLog(@"InterruptionTypeBegan");
        } else {
            // the facetime iOS 9 has a bug: 1 does not send interrupt end 2 you can use application become active, and repeat set audio session acitve until success.  ref http://blog.corywiles.com/broken-facetime-audio-interruptions-in-ios-9
            NSLog(@"InterruptionTypeEnded");
            AVAudioSession *session = [AVAudioSession sharedInstance];
            [session setActive:YES error:nil];
        }
    }
}


//私有方法
//被设为管理员
-(void)adminWarning
{
    UIAlertController *tipAddfriend = [UIAlertController alertControllerWithTitle:NSLTLocalizedString(@"提示", nil) message:NSLTLocalizedString(@"恭喜您被设置为管理员", nil) preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"好的", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [tipAddfriend addAction:sureAction];
    [self presentViewController:tipAddfriend animated:YES completion:nil];
}


//被设为黑名单
-(void)blackListWarning
{
    UIAlertController *tipAddfriend = [UIAlertController alertControllerWithTitle:NSLTLocalizedString(@"提示", nil) message:NSLTLocalizedString(@"您已被禁言", nil) preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"好的", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }];
    [tipAddfriend addAction:sureAction];
    [self presentViewController:tipAddfriend animated:YES completion:nil];
}

//私有方法
-(void)failedWarning:(NSString *)warningString withInfo:(NSString *)info
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:warningString preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:NSLTLocalizedString(@"确 定", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([info isEqualToString:@"adminLimited"] ||[info isEqualToString:@"netWarning"] ) {
            
        }else if([info isEqualToString:@"creatFailed"]){
//            [self close]
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{

        }
    }];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)allDirection
{
    //在视图出现的时候，将allowRotate改为1，
//    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    delegate.allowRotate = 1;
    
}

// 懒加载
-(BulletManager *)bulletManager
{
    if(_bulletManager == nil)
    {
        _bulletManager = [[BulletManager alloc]init];
        __weak HBaseLiveController *myself = self;
        _bulletManager.generateBulletBlock = ^(BulletView *bulletView) {
            [myself addBulletView:bulletView];
        };
    }
    _bulletManager.roomID = self.roomid;
    return _bulletManager;
}
-(HHugeAnimationManger *)hugeAimationManger
{
    if(_hugeAimationManger == nil)
    {
        _hugeAimationManger = [HHugeAnimationManger hhugeAnimation];
        _hugeAimationManger.presentView = self.mainView;
    }
    return _hugeAimationManger;
}


- (void)addBulletView:(BulletView *)bulletView  {
    bulletView.frame = CGRectMake(CGRectGetWidth(self.view.frame)+50, HEIGHT/2 - 150 + 34 * bulletView.trajectory, CGRectGetWidth(bulletView.bounds), CGRectGetHeight(bulletView.bounds));
    [self.mainView addSubview:bulletView];
    [bulletView startAnimation];
}

-(SendGiftView *)sendgiftView
{
    if(_sendgiftView == nil)
    {
        _sendgiftView = [SendGiftView sendGiftView];
        _sendgiftView.x = -200;
        _sendgiftView.y = 110;
        _sendgiftView.ishidden = YES;
        _sendgiftView.hidden = YES;
        [self.mainView addSubview:_sendgiftView];
    }
    return _sendgiftView;
}
-(SendGiftView *)sendgiftView1
{
    if(_sendgiftView1 == nil)
    {
        _sendgiftView1 = [SendGiftView sendGiftView];
        _sendgiftView1.x = -200;
        _sendgiftView1.y = 110 + 60;
        _sendgiftView1.ishidden = YES;
        _sendgiftView1.hidden = YES;
        [self.mainView addSubview:_sendgiftView1];
    }
    return _sendgiftView1;
}
-(HReplayProgressView *)replayProgressView
{
    if(_replayProgressView == nil)
    {
        _replayProgressView = [[HReplayProgressView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _replayProgressView.delegate = self;
        
    }
    return _replayProgressView;
}
//-(PersonDetail *)personDetail
//{
//    if(_personDetail == nil)
//    {
//        _personDetail = [PersonDetail initPersonalViewwihtframe:CGRectMake(0, 0, 300, 300)];
//        _personDetail.delegate = self;
//        _personDetail.presentController = self;
//    }
//    return _personDetail;
//}
//-(PersonalDetailForHost *)personDetailForhost
//{
//    if(_personDetailForhost == nil)
//    {
//        _personDetailForhost = [PersonalDetailForHost initPersonalViewwihtframe:CGRectMake(0, 0, 300, 300)];
//        _personDetailForhost.delegate = self;
//        _personDetailForhost.presentController = self;
//        _personDetailForhost.roomID = self.roomid;
//    }
//    return _personDetailForhost;
//}
-(StopLiveView *)livestopView
{
    if(_livestopView == nil)
    {
        _livestopView = [StopLiveView setopliveView];
        _livestopView.delegate = self;
    }
    return _livestopView;
}
-(HLiveFinishView *)finishView
{
    if(_finishView == nil)
    {
        _finishView = [HLiveFinishView hlivefinishView];
        _finishView.delegate = self;
        
    }
    return _finishView;
}

-(MTLiveFunctionView *)functionView
{
    if(_functionView == nil)
    {
        _functionView = [[MTLiveFunctionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
        _functionView.delegate = self;
    }
    return _functionView;
}
-(MTBeautifySetView *)beautifySetView
{
    if(_beautifySetView == nil)
    {
        _beautifySetView = [MTBeautifySetView beautifySetView];
        _beautifySetView.delegate = self;
    }
    return _beautifySetView;
}

-(HLiveSendGiftView *)giftView
{
    if(_giftView == nil)
    {
        _giftView = [HLiveSendGiftView sendGiftView];
        _giftView.targetViewcontroller = self;
        _giftView.delegate = self;
    }
    return _giftView;
}
-(MTLiveProfileView *)userDetailView
{
    if(_userDetailView == nil)
    {
        _userDetailView = [MTLiveProfileView liveProfileView];
        _userDetailView.delegate = self;
        _userDetailView.targetVc = self;
    }
    return _userDetailView;
}

-(MTCreateLiveView *)createLiveView
{
    if(_createLiveView == nil)
    {
        _createLiveView = [MTCreateLiveView createLiveView];
        _createLiveView.delegate = self;
    }
    return _createLiveView;
}


-(NSArray *)videoStreamingConfigurations
{
    if(_videoStreamingConfigurations == nil)
    {

        _videoStreamingConfigurations = @[

//                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:10 videoMaxKeyframeInterval:30 averageVideoBitRate:300 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
//                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:15 videoMaxKeyframeInterval:45 averageVideoBitRate:400 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:18 videoMaxKeyframeInterval:48 averageVideoBitRate:500 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:20 videoMaxKeyframeInterval:60 averageVideoBitRate:650 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:24 videoMaxKeyframeInterval:72 averageVideoBitRate:700 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:24 videoMaxKeyframeInterval:81 averageVideoBitRate:750 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:27 videoMaxKeyframeInterval:90 averageVideoBitRate:800 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:30 videoMaxKeyframeInterval:90 averageVideoBitRate:850 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox],
                                           [[PLVideoStreamingConfiguration alloc]initWithVideoSize:CGSizeMake(WIDTH, HEIGHT) expectedSourceVideoFrameRate:30 videoMaxKeyframeInterval:90 averageVideoBitRate:900 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31 videoEncoderType:PLH264EncoderType_VideoToolbox]

//
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:10 videoMaxKeyframeInterval:30 averageVideoBitRate:500 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:15 videoMaxKeyframeInterval:45 averageVideoBitRate:500 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:18 videoMaxKeyframeInterval:48 averageVideoBitRate:600 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:20 videoMaxKeyframeInterval:60 averageVideoBitRate:700 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:24 videoMaxKeyframeInterval:72 averageVideoBitRate:700 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:27 videoMaxKeyframeInterval:81 averageVideoBitRate:800 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:30 videoMaxKeyframeInterval:90 averageVideoBitRate:900 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:30 videoMaxKeyframeInterval:90 averageVideoBitRate:900 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31],
//                                         [[PLVideoStreamingConfiguration alloc] initWithVideoSize:self.view.bounds.size expectedSourceVideoFrameRate:30 videoMaxKeyframeInterval:90 averageVideoBitRate:900 * 1000 videoProfileLevel:AVVideoProfileLevelH264Baseline31]
//
                                         ];
    }
    return _videoStreamingConfigurations;
}

-(NSArray *)videoCaptureConfigurations
{
    if(_videoCaptureConfigurations == nil)
    {
        _videoCaptureConfigurations = @[
//                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:12 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
//                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:18 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:21 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:24 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:27 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:30 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:38 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:40 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait],
                                        [[PLVideoCaptureConfiguration alloc] initWithVideoFrameRate:40 sessionPreset:AVCaptureSessionPresetiFrame1280x720 previewMirrorFrontFacing:YES previewMirrorRearFacing:NO streamMirrorFrontFacing:YES streamMirrorRearFacing:NO cameraPosition:AVCaptureDevicePositionFront videoOrientation:AVCaptureVideoOrientationPortrait]
                                        ];

    }
    return _videoCaptureConfigurations;
}
-(void)startAnimation
{
    [self.animationView startAnimating];
    self.animationView.hidden = NO;
}

-(void)stopAnimation
{
    [self.animationView stopAnimating];
     self.animationView.hidden = YES;
}
-(UIActivityIndicatorView *)animationView
{
    if (!_animationView) {
        
        _animationView =  [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhite)];
       _animationView.center = self.view.center;
        
//        NSMutableArray * arrayImages = [NSMutableArray array];
//        for (int i = 1; i < 25; i++) {
//            UIImage * image=[UIImage imageNamed:[NSString stringWithFormat:@"a%d",i]];
//            [arrayImages addObject:image];
//        }
//        _animationView  = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 88, 88)];
//
//        _animationView.animationImages = arrayImages;
//        _animationView.animationDuration = 0.05 * arrayImages.count;
        [self.view addSubview:_animationView];
    }
    
    return _animationView;
}
-(void)dealloc
{
   
    self.inputView = nil;
    self.personDetail = nil;
    
    
}

@end
